﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ActiveForums.Controls
    <DefaultProperty("Text"), ToolboxData("<{0}:Members runat=server></{0}:Members>")> _
    Public Class Members
        Inherits SettingsBase
#Region "Private Members"
        Private _memberCount As Integer = 0

        Private PageSize As Integer = 20
        Private RowIndex As Integer = 0
        Private Filter As String = ""
#End Region
#Region "Public Properties"
#End Region
#Region "Protected Controls"
        Protected WithEvents plhContent As New PlaceHolder
        Protected WithEvents Pager1 As Global.DotNetNuke.Modules.ActiveForums.Controls.PagerNav
#End Region
#Region "Event Handlers"

        Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Me.AppRelativeVirtualPath = "~/"
            If Not Request.Params("affilter") Is Nothing Then
                Filter = CType(Request.Params("affilter"), String).Chars(0)
            Else
                Filter = String.Empty
            End If
        End Sub
        Private Sub Page_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try
                BuildControl()
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
            End Try
            Try
                LinkControls(Controls)
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
            End Try
            Try
                BuildPager()
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
            End Try

        End Sub
#End Region
#Region "Private Methods"
        Private Sub BuildControl()

            Dim sb As New Text.StringBuilder(1024)
            Dim sTemplate As String = String.Empty
            If IO.File.Exists(Server.MapPath(ImagePath & "/_memberlist.txt")) Then
                sTemplate = Utilities.GetFileContent(Server.MapPath(ImagePath & "/_memberlist.txt"))
            Else
                sTemplate = Utilities.GetFileContent(Server.MapPath("~/DesktopModules/ActiveForums/config/templates/_memberlist.txt"))
            End If


            If Not sTemplate = String.Empty Then
                Dim sGrid As String = TemplateUtils.GetTemplateSection(sTemplate, "[AF:CONTROL:LIST]", "[/AF:CONTROL:LIST]")
                Dim sHeader As String = TemplateUtils.GetTemplateSection(sTemplate, "[AF:CONTROL:LIST:HEADER]", "[/AF:CONTROL:LIST:HEADER]")
                Dim sNormRow As String = TemplateUtils.GetTemplateSection(sTemplate, "[AF:CONTROL:LIST:ITEM]", "[/AF:CONTROL:LIST:ITEM]")
                Dim sAltRow As String = TemplateUtils.GetTemplateSection(sTemplate, "[AF:CONTROL:LIST:ALTITEM]", "[/AF:CONTROL:LIST:ALTITEM]")
                Dim sFooter As String = TemplateUtils.GetTemplateSection(sTemplate, "[AF:CONTROL:LIST:FOOTER]", "[/AF:CONTROL:LIST:FOOTER]")
                sGrid = TemplateUtils.ReplaceSubSection(sGrid, sHeader, "[AF:CONTROL:LIST:HEADER]", "[/AF:CONTROL:LIST:HEADER]")
                sGrid = TemplateUtils.ReplaceSubSection(sGrid, sFooter, "[AF:CONTROL:LIST:FOOTER]", "[/AF:CONTROL:LIST:FOOTER]")

                Dim upl As List(Of User) = GetMemberList()
                If Not upl Is Nothing Then
                    Dim i As Integer = 0
                    For Each up As User In upl
                        Dim sRow As String = String.Empty
                        If i Mod 2 = 0 Then
                            sRow = sNormRow
                        Else
                            sRow = sAltRow
                        End If
                        sRow = TemplateUtils.ParseProfileTemplate(sRow, up, PortalId, ModuleId, ImagePath, CurrentUserType, TimeZoneOffset)
                        sb.Append(sRow)
                        i += 1
                    Next
                    sGrid = TemplateUtils.ReplaceSubSection(sGrid, sb.ToString, "[AF:CONTROL:LIST:ITEM]", "[/AF:CONTROL:LIST:ALTITEM]")
                End If
                sTemplate = TemplateUtils.ReplaceSubSection(sTemplate, sGrid, "[AF:CONTROL:LIST]", "[/AF:CONTROL:LIST]")
            End If
            sTemplate = Globals.ControlRegisterTag & sTemplate
            sTemplate = sTemplate.Replace("[AF:CONTROL:PAGER]", "<am:pagernav id=""Pager1"" runat=""server"" />")
            sTemplate = sTemplate.Replace("[AF:CONTROL:ALPHABAR]", BuildAlphaList())
            Dim ctl As Control = Me.ParseControl(sTemplate)

            Me.Controls.Add(ctl)
        End Sub
        Private Sub LinkControls(ByVal ctrls As ControlCollection)
            For Each ctrl As Control In ctrls
                Select Case ctrl.ID
                    Case "Pager1"
                        Pager1 = CType(ctrl, DotNetNuke.Modules.ActiveForums.Controls.PagerNav)
                End Select
                If ctrl.Controls.Count > 0 Then
                    LinkControls(ctrl.Controls)
                End If
            Next
        End Sub
        Private Sub BuildPager()
            Dim intPages As Integer
            intPages = CType(System.Math.Ceiling(_memberCount / PageSize), Integer)
            Pager1.PageCount = intPages
            Pager1.CurrentPage = PageId
            Pager1.TabID = TabId
            Pager1.ForumID = -1
            Pager1.PageText = Utilities.GetSharedResource("[RESX:Page]")
            Pager1.OfText = Utilities.GetSharedResource("[RESX:PageOf]")
            Pager1.View = "members"
            If UseAjax Then
                Pager1.PageMode = PagerNav.Mode.CallBack
            Else
                Pager1.PageMode = PagerNav.Mode.Links
            End If
            If Not Request.Params("affilter") Is Nothing Then
                Dim Params As String() = {"affilter=" & Request.Params("affilter")}
                Pager1.Params = Params
            End If
        End Sub
        Private Function BuildAlphaList() As String
            Dim sb As New Text.StringBuilder
            Dim litString As New LiteralControl
            sb.Append("<div align=""center""><table width=""90%"" cellpadding=""0"" cellspacing=""5""><tr>")
            Dim i As Integer = 65
            Dim hypAlpha As New HyperLink
            For i = 65 To 90
                litString = New LiteralControl
                sb.Append("<td align=""center"">")
                Dim strChar As String
                strChar = Chr(i)
                Dim Params() As String = {ParamKeys.ViewType & "=members", "affilter=" & strChar}
                sb.Append("<a href=""" & NavigateUrl(TabId, "", Params) & """ class=""CommandButton"">")
                sb.Append(strChar)
                sb.Append("</a></td>")
            Next
            sb.Append("<td align=center>")
            sb.Append("<a href=""" & NavigateUrl(TabId, "", ParamKeys.ViewType & "=members") & """ class=""CommandButton"">")
            sb.Append(Utilities.GetSharedResource("[RESX:All]"))
            sb.Append("</a></td></tr></table></div>")
            Return sb.ToString
        End Function
        Private Function GetMemberList() As List(Of User)
            Dim upl As New List(Of User)
            Dim upi As User = Nothing
            PageSize = MainSettings.PageSize
            If PageId = 1 Then
                RowIndex = 0
            Else
                RowIndex = ((PageId * PageSize) - PageSize)
            End If
            Dim dr As IDataReader = DataProvider.Instance.Profiles_MemberList(PortalId, ModuleId, PageSize, RowIndex, Filter)
            Try
                dr.Read()
                _memberCount = CInt(dr(0))
                dr.NextResult()
                While dr.Read()
                    upi = New User
                    With upi
                        .UserId = CInt(dr("UserId").ToString)
                        With upi.Profile
                            .PortalId = CInt(dr("PortalId").ToString)
                            .TopicCount = CInt(dr("TopicCount").ToString)
                            .ReplyCount = CInt(dr("ReplyCount").ToString)
                            .ViewCount = CInt(dr("ViewCount").ToString)
                            .AnswerCount = CInt(dr("AnswerCount").ToString)
                            .RewardPoints = CInt(dr("RewardPoints").ToString)
                            .UserCaption = CStr(dr("UserCaption").ToString)
                            .IsUserOnline = CBool(dr("IsUserOnline").ToString)
                            If Not dr("DateCreated").ToString = String.Empty Then
                                .DateCreated = CDate(dr("DateCreated").ToString)
                            End If
                            If Not dr("DateUpdated").ToString = String.Empty Then
                                .DateUpdated = CDate(dr("DateUpdated").ToString)
                            End If
                            If Not dr("DateLastActivity").ToString = String.Empty Then
                                .DateLastActivity = CDate(dr("DateLastActivity").ToString)
                            End If
                            .Signature = CStr(dr("Signature").ToString)
                            If Not dr("SignatureDisabled").ToString = String.Empty Then
                                .SignatureDisabled = CBool(dr("SignatureDisabled").ToString)
                            End If
                            If Not dr("TrustLevel").ToString = String.Empty Then
                                .TrustLevel = CInt(dr("TrustLevel").ToString)
                            End If
                            If Not dr("AdminWatch").ToString = String.Empty Then
                                .AdminWatch = CBool(dr("AdminWatch").ToString)
                            End If
                            If Not dr("AttachDisabled").ToString = String.Empty Then
                                .AttachDisabled = CBool(dr("AttachDisabled").ToString)
                            End If
                            .Avatar = CStr(dr("Avatar").ToString)
                            If Not dr("AvatarType").ToString = String.Empty Then
                                .AvatarType = CType(dr("AvatarType").ToString, AvatarTypes)
                            End If
                            If Not dr("AvatarDisabled").ToString = String.Empty Then
                                .AvatarDisabled = CBool(dr("AvatarDisabled").ToString)
                            End If
                            .Yahoo = CStr(dr("Yahoo").ToString)
                            .MSN = CStr(dr("MSN").ToString)
                            .ICQ = CStr(dr("ICQ").ToString)
                            .AOL = CStr(dr("AOL").ToString)
                            .Occupation = CStr(dr("Occupation").ToString)
                            .Location = CStr(dr("Location").ToString)
                            .Interests = CStr(dr("Interests").ToString)
                            .WebSite = CStr(dr("WebSite").ToString)
                            .Badges = CStr(dr("Badges").ToString)
                            .Bio = CStr(dr("Bio").ToString)
                            If Not dr("PrefBlockAvatars").ToString = String.Empty Then
                                .PrefBlockAvatars = CBool(dr("PrefBlockAvatars").ToString)
                            Else
                                .PrefBlockAvatars = False
                            End If

                            If Not dr("DateLastPost").ToString = String.Empty Then
                                .DateLastPost = CDate(dr("DateLastPost").ToString)
                            End If
                        End With
                       
                       
                        .Username = CStr(dr("Username").ToString)
                        .FirstName = CStr(dr("FirstName").ToString)
                        .LastName = CStr(dr("LastName").ToString)
                        .Email = CStr(dr("Email").ToString)
                        .DisplayName = CStr(dr("DisplayName").ToString)
                      



                    End With
                    upl.Add(upi)
                End While
            Catch ex As Exception
                dr.Close()
                dr = Nothing
            Finally
                If Not dr.IsClosed Then
                    dr.Close()
                    dr = Nothing
                End If
            End Try
            Return upl
        End Function

#End Region
    End Class
End Namespace